package test.deque;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;

public class BracketValidator {

    public static boolean isValid(String s) {
        Deque<Character> stack = new ArrayDeque<>();
        Map<Character, Character> map = Map.of(
                ')', '(',
                ']', '[',
                '}', '{'
        );

        for (char c : s.toCharArray()) {
            if (map.containsValue(c)) {
                stack.push(c);
            } else if (map.containsKey(c)) {
                if (stack.isEmpty() || stack.pop() != map.get(c)) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("({[]})"));  // true
        System.out.println(isValid("([)]"));    // false
        System.out.println(isValid("[]"));    // true
        System.out.println(isValid("{}"));    // true
    }
}
